import { Component, OnInit } from '@angular/core';
import { PickerController, NavController, AlertController } from '@ionic/angular';
import { ToolService } from 'src/app/service/tool.service';
import { HttpService } from 'src/app/service/http.service';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { Router, ActivatedRoute, Params } from '@angular/router';

@Component({
  selector: 'app-transaction',
  templateUrl: './transaction.page.html',
  styleUrls: ['./transaction.page.scss'],
})
export class TransactionPage implements OnInit {

  startData: string = ToolService.getNowFormatDate();
  endData: string = ToolService.getNowFormatDate();
  public selectType: any = "all";  //选择的充值类型
  public timeType="1";   //选择的时间类型
  public listData: any[] = [];//所有的数据
  public credit2: any; //可用余额
  public credit_expend3: any;//未结金额
  public credit_total: any;//账号总额

  private edate="";
  private sdate="";

  minValue = "2019-01-01";
  constructor(private pickercontroller: PickerController, private navCtrl: NavController, private http: HttpService, private popWin: PopwinService, private router: Router, private activaRoute: ActivatedRoute,
    private alertController:AlertController) {
    this.activaRoute.queryParams.subscribe((params: Params) => {
      let tempDeal=JSON.parse(ToolService.paramAESDecrypt(params['p']));
      if (!ToolService.isEmpty(tempDeal)) {
        this.selectType = tempDeal['type'];
        this.selectDay();
      }
    });
  }

  ngOnInit() {
    this.getAssets();
  }
 
 

  backClick() {
    this.navCtrl.navigateRoot("/tabs/recharge");
  }


  selectDay() {
    let that = this;
    let obj = {};
    if(that.timeType==='self'){ //自选时间  
      obj = "{\"signid\":\"" + that.selectType + "\",\"sdate\":\"" + that.startData.substr(0, 10) + "\",\"edate\":\"" + that.edate.substr(0, 10) + "\",\"dateType\":\"5\"}";
    }else{
      obj = "{\"signid\":\"" + that.selectType + "\",\"dateType\":\"" + that.timeType + "\"}";
    }
    this.http.get(UrlService.MENU_SHOWACCTLISTS, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let res = ToolService.aesDecodeToJson(temp);
        that.listData = res.rowset;
        that.sdate=res.sdate;
        that.edate=res.edate;
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  getAssets() {
    let that = this;
    this.http.get(UrlService.USER_FINDCREDIT, { "callback": "JSONP_CALLBACK" }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let res = ToolService.aesDecodeToJson(temp);
        that.credit2 = res.credit2; //可用余额
        that.credit_expend3 = res.credit_expend3;//未结金额
        that.credit_total = res.credit2 + res.credit_expend3;//账号总额
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  /**
   * 选中后的一个跳转到具体的详细数据列表
   * @param signid 
   */
  selectDetail(signid) {
    let that = this;
    let param = "{\"signid\":\"" + signid + "\",\"stime\":\"" + this.sdate + "\",\"etime\":\"" + this.edate + "\"}";
    this.router.navigate(['/transactionlist'], {
      queryParams: { "p": ToolService.paramAESEncrypt(param)}
    });
  }

  onSelectType(signid){
     if(signid!==this.selectType){
        this.selectType=signid;
        this.selectDay();
     }
  }

  onSelectTime(timeType){
     if(timeType!==this.timeType && timeType!=="self"){
        this.timeType=timeType;
        this.selectDay();
     }
     this.timeType=timeType;
     if(timeType==="self"){
        this.presentAlertPrompt();
     }
  }


  async presentAlertPrompt() {
    let that=this;
    const alert = await this.alertController.create({
      header: '选择日期查询',
      inputs: [
        {
          name: 'startTime',
          type: 'date',
          value: that.startData,
          label:'开始'
        },
        {
          name: 'endTime',
          type: 'date',
          value: that.endData,
          label:'结束'
        }
      ],
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'firstBtn',
          handler: () => {
             
          }
        }, {
          text: '确定',
          cssClass: 'secondBtn',
          handler: (data) => {
             that.startData=data.startTime;
             that.endData=data.endTime;
             that.selectDay();
          }
        }
      ]
    });

    await alert.present();
  }



  onChangeTime(){
     this.selectDay();
  }


}
